package com.personal.website.lottery.web.controller;

import com.personal.website.lottery.web.model.Article;
import com.personal.website.lottery.web.model.Comment;
import com.personal.website.lottery.web.model.User;
import com.personal.website.lottery.web.service.ArticleService;
import com.personal.website.lottery.web.service.CommentService;
import com.personal.website.lottery.web.service.UserService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 10171225 on 2016/2/18.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;
    @Resource
    private UserService userService;
    @Resource
    private CommentService commentService;

    @RequestMapping("/create")
    public String create(HttpSession session, HttpServletRequest request, Model model) {
        String content = request.getParameter("content");
        String tag = request.getParameter("tag");
        String title = request.getParameter("title");
        int userId = (Integer) session.getAttribute("userId");
        String userName = (String) session.getAttribute("userName");
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setIsReviewed((byte)0);
        article.setIsTop((byte)0);
        article.setSubmitTime(new Date());
        article.setTag(tag);
        article.setUserId(userId);
        article.setUserName(userName);
        this.articleService.insertArticle(article);
        model.addAttribute("backMessage","文章发表成功，请等待审核！");
        return "backMessage";
    }

    @RequestMapping("/show")
    public String show(HttpServletRequest request,Model model) {
        int articleId = Integer.parseInt(request.getParameter("articleId"));
        Article article = this.articleService.selectArticleById(articleId);
        User user = this.userService.getUserById(article.getUserId());
        List<Comment> commentLst = this.commentService.getCommentsByArcicleId(articleId);
        model.addAttribute("article",article);
        model.addAttribute("user",user);
        model.addAttribute("commentLst",commentLst);

        return "showArticle";
    }

    @RequestMapping("/get")
    @ResponseBody
    public Article get() {
        Article article = this.articleService.getArticles(1).get(0);
        System.out.println("get:" + article.getTitle());
        return article;
    }

    @RequestMapping("/editShow")
    public String editShow(HttpServletRequest request,HttpSession session,Model model) {
        int articleId = Integer.parseInt(request.getParameter("articleId"));
        int userId = (Integer) session.getAttribute("userId");
        Article article = this.articleService.selectArticleById(articleId);
        if(article != null && !article.getUserId().equals(userId)){
            model.addAttribute("backMessage","您没有权限修改该文章！");
            return "backMessage";
        } else if (article == null) {
            model.addAttribute("backMessage","没有找到该文章！");
            return "backMessage";
        }
        model.addAttribute("article",this.articleService.selectArticleById(articleId));
        return "editArticle";
    }

    @RequestMapping("/editSubmit")
    public String editSubmit(HttpServletRequest request,HttpSession session,Model model) {
        String articleId = request.getParameter("articleId");
        String title = request.getParameter("title");

        String content = request.getParameter("content");
        String tag = request.getParameter("tag");

        Article article = this.articleService.selectArticleById(Integer.parseInt(articleId));
        if(article.getUserId() != (Integer) session.getAttribute("userId")){
            model.addAttribute("backMessage","您不是该文章的创建人，您没有权限编辑！");
            return "backMessage";
        }
        article.setContent(content);
        article.setIsReviewed((byte) 0);
        article.setTag(tag);
        article.setTitle(title);
        if(!this.articleService.updateArticle(article)){
            model.addAttribute("backMessage","保存异常，编辑失败！");
            return "backMessage";
        }
        model.addAttribute("backMessage","编辑成功，请等待重新审核！");
        return "backMessage";
    }

    @RequestMapping("/delete")
    public String delete(HttpServletRequest request,HttpSession session,Model model) {
        String articleId = request.getParameter("articleId");
        Article article = this.articleService.selectArticleById(Integer.parseInt(articleId));
        if(article == null){
            model.addAttribute("backMessage","不存在该文章！");
            return "backMessage";
        }

        if(article.getUserId() != (Integer) session.getAttribute("userId")){
            model.addAttribute("backMessage","你不是该文章的创建人，没有权限删除！");
            return "backMessage";
        }
        if(!this.articleService.deleteArticle(Integer.parseInt(articleId))){
            model.addAttribute("backMessage","没有删除成功！");
            return "backMessage";
        }
        model.addAttribute("backMessage","删除成功！");
        return "backMessage";
    }

    @RequestMapping("/comment")
    public String comment(HttpServletRequest request,HttpSession session,Model model) {
        int articleId = Integer.parseInt(request.getParameter("articleId"));
        int userId = (Integer) session.getAttribute("userId");
        String userName = (String) session.getAttribute("userName");
        String content = request.getParameter("commentContent");
        Comment comment = new Comment();
        comment.setArticleId(articleId);
        comment.setUserId(userId);
        comment.setUserName(userName);
        comment.setContent(content);
        comment.setSubmitTime(new Date());
        this.commentService.insertComment(comment);
        model.addAttribute("backMessage","评论成功！");
        return "backMessage";
    }
}
